<?php

class phpinchains_util_ValidatorHelper
{

    /**
     * Parsing of validation param string
     * 
     * Validation param string is of format:
     * name1:value1;name2:value2[;...]
     *
     * @param string $string		ValidationParam string
     * @param string $validatorName Name of validator used with error messages
     * @param array $requiredParams Names of the required params (missing params fire an error)
     * @return array Params as name value pairs
     */
    function parseValidationParam ($string, $validatorName = '', $requiredParams = array())
    {
        $params = preg_split("/(?<!\\\);/", $string);
        $nvparams = array();
        foreach ($params as $param)
        {
            $pair = preg_split("/(?<!\\\):/", $param);
            $nvparams[$pair[0]] = str_replace("\\\:",":",str_replace("\\\;",";",$pair[1]));
        }

        if (is_array($requiredParams))
        {
        	foreach ($requiredParams as $rparam)
	        {
    	    	if (!isset($nvparams[$rparam])) 
        		{
        			$ctx = &phpinchains_ApplicationContext::getInstance();
	        		$out = &$ctx->getOutputWriter();
    	    		$out->write("<strong>Validator '$validatorName' requires param '$rparam'.</strong><br/>\n");
        		}
	        }
        }
        
	    if (isset($nvparams['allow_empty']) && $nvparams['allow_empty'] == 'true')
	    {	$nvparams['allow_empty'] = true; }
       	else
       	{	$nvparams['allow_empty'] = false; }

       	return $nvparams;
    }

    function atLeastOneOf ($fields, & $actionform)
    {
        foreach ($fields as $field)
        {
            if ($actionform->get($field))
                return true;
        }
        return false;
    }

}

?>
